using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using PriorityQueue;

namespace MUSA
{
    public class BoundingSphereVisual : Object
    {
        #region Memeber Variables

        protected float radius;

        int pointCount;
        VertexPositionNormalTexture[] pointList;
        VertexBuffer vertexBuffer;
        
        #endregion

        #region Constructor

        BoundingSphereVisual(Game game)
            : base(game)
        {
        }

        public BoundingSphereVisual(Game game, Vector3 _position, float _radius)
            :base(game)
        {
            position = _position;
            radius = _radius;

            pointCount = 8;
            float segmentAngle = MathHelper.TwoPi / pointCount;
            pointList = new VertexPositionNormalTexture[pointCount];

            for (int i = 0; i < pointCount; i++)
            {
                pointList[i] = new VertexPositionNormalTexture( new Vector3( radius * (float)Math.Round(Math.Cos(segmentAngle * i)), radius * (float)Math.Round(Math.Sin(segmentAngle * i)), 0), Vector3.Forward, new Vector2());
            }

            IGraphicsDeviceService graphics = (IGraphicsDeviceService)mGame.Services.GetService(typeof(IGraphicsDeviceService));

            vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionNormalTexture.SizeInBytes * pointList.Length, ResourceUsage.None, ResourceManagementMode.Automatic);
            vertexBuffer.SetData<VertexPositionNormalTexture>(pointList);

            graphics.GraphicsDevice.RenderState.PointSize = 10;

            
        }

        #endregion

        #region Methods

        protected override void Create()
        {
            IGraphicsDeviceService graphics = (IGraphicsDeviceService)mGame.Services.GetService(typeof(IGraphicsDeviceService));

            #region Initialize Basic Effects

            //Initialize the Basic Effect
            basicEffect = new BasicEffect(graphics.GraphicsDevice, null);

            basicEffect.Alpha = 1.0f;
            basicEffect.DiffuseColor = new Vector3(1.0f, 0.0f, 1.0f);
            basicEffect.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f);
            basicEffect.SpecularPower = 5.0f;
            basicEffect.AmbientLightColor = new Vector3(0.75f, 0.75f, 0.75f);

            basicEffect.DirectionalLight0.Enabled = true;
            basicEffect.DirectionalLight0.DiffuseColor = Vector3.One;
            basicEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1.0f, -1.0f, -1.0f));
            basicEffect.DirectionalLight0.SpecularColor = Vector3.One;

            basicEffect.DirectionalLight1.Enabled = true;
            basicEffect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
            basicEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
            basicEffect.DirectionalLight1.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f);

            basicEffect.LightingEnabled = true;

            basicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, 50), Vector3.Zero, Vector3.Up);
            basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(45),  // 45 degree angle
            (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
            1.0f, 100.0f);
            //MATRIX//-------

            #endregion Initialize Basic Effects

        }

        public override void Draw(GameTime gTime)
        {
            IGraphicsDeviceService graphics = (IGraphicsDeviceService)mGame.Services.GetService(typeof(IGraphicsDeviceService));

            graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
            graphics.GraphicsDevice.DrawPrimitives( PrimitiveType.PointList, 0, 7);
        }

        #endregion
    }
}
